/**
 * This class is an session bean class
 * that performes all the oprations for 
 * the communications entities.
 * It implements the IPerformeCommunication interface
 */
package com.teamreinforce.ejb.model;

import com.teamreinforce.ejb.beans.CollaborateurFacade;
import com.teamreinforce.ejb.beans.NotificationFacade;
import com.teamreinforce.ejb.beans.ReunionFacade;
import com.teamreinforce.ejb.entities.*;
import com.teamreinforce.utils.TeamReinforceTimeUtils;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author tidiane
 */
@Stateless
public class PerformeCommunication implements IPerformeCommunication {

    @PersistenceContext ( unitName = "TeamreinforceEJBPU")
    private EntityManager em;
    
    @EJB
    private ReunionFacade reunionF;
    
    @EJB
    private CollaborateurFacade collaboratorF;
    
    @EJB
    private NotificationFacade notificationF;
    @Override
    public void createMeeting(Projet project, List<Collaborateur> participants, String titre, String description, String dateReunion, String heureDebut, String heureFin, String salle) {
        // Create the meeting.
        int id = reunionF.count() + 1;
        Reunion _reunion = new Reunion(id, titre, dateReunion, heureDebut, heureFin);
        if ( description != null && ! description.isEmpty()) _reunion.setDescription(description);
        if ( salle != null && ! salle.isEmpty()) _reunion.setSalleReunion(salle);
        
        // Set with the project
        _reunion.setProjet(project);
        // then persist
        reunionF.create(_reunion);

        Reunion r = reunionF.find(id);
        for ( Collaborateur c : participants) {
            Query query = em.createNativeQuery("INSERT INTO assiste VALUES (" + c.getIdCollaborateur() + ", " + r.getIdReunion() +")");
            query.setParameter("id", c.getIdCollaborateur());
            query.setParameter(":id_reu", _reunion.getIdReunion());
            query.executeUpdate();
            notificate(c, "Vous avec une nouvelle : Projet réunion [" + project.getTitre() +" ]");
            collaboratorF.edit(c);
        }
        
    }
 
    @Override
    public void createSubject(Collaborateur collaborator, Projet project, String titre, String sujet) {
        Sujet _subject = new Sujet(Integer.SIZE);
        _subject.setDate(TeamReinforceTimeUtils.DATE);
        _subject.setCollaborateur(collaborator);
        _subject.setProjet(project);
        em.persist(titre);
        
        // TODO : Make notifications ...
    }

    @Override
    public void postResponse(Collaborateur collaborator, Sujet subject, String message) {
        Reponse _response = new Reponse(Integer.MIN_VALUE, message, TeamReinforceTimeUtils.DATE);
        _response.setSujet(subject);
        _response.setCollaborateur(collaborator);
        
        // Making the comment persistant
        em.persist(_response);
    }

     /**
     * Add the new notification
     * of the given collaboratorme parametre une tache et colla
     * @param collaborator
     * @param notificationMessage 
     */
    public void notificate(Collaborateur collaborator, String notification){
        Notification _notification = new Notification(Integer.MIN_VALUE, TeamReinforceTimeUtils.DATE + " à " + TeamReinforceTimeUtils.TIME, notification);
        _notification.setCollaborateur(collaborator);
        notificationF.create(_notification);
        collaboratorF.edit(collaborator);
    }  
}
